package com.cnhis.cloudhealth.clinical.infusion.base.bo;

import com.cnhis.cloudhealth.clinical.infusion.base.dao.CliSysParametersDao;
import com.cnhis.cloudhealth.clinical.infusion.base.dao.InfusionDao;
import com.cnhis.cloudhealth.clinical.infusion.base.mapper.SysParameters;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.cardmember.bo.CardMemberMangerBo;
import com.cnhis.cloudhealth.commons.commonSql.bo.CommonSqlBo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdd on 2016/9/18.
 */
@Service
public class InfusionBo {
    @Autowired
    private InfusionDao infusionDao;
    @Autowired
    private GetNewId getNewId;
    @Autowired
    private CliSysParametersDao clisysParametersDao;
    @Autowired
    private CardMemberMangerBo cardMemberMangerBo;
    @Autowired
    private CommonSqlBo commonSqlBo;

    /**
     * 查询病人列表信息 （输液待执行病人）
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findPatientList(Map<String, Object> map) throws Exception {
        return infusionDao.findPatientList(map);
    }

    /**
     * 查询病人列表信息 （输液 已完成  病人）
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findCompletedPatientList(Map<String, Object> map) throws Exception {
        return infusionDao.findCompletedPatientList(map);
    }

    /**
     * 查询病区科室
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDept(Map<String, Object> map) throws Exception {
        return infusionDao.queryDept(map);
    }

    /**
     * 根据 就诊id  查询此病人的详细信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDetailPatient(Map<String, Object> map) throws Exception {
        return infusionDao.queryDetailPatient(map);
    }

    /**
     * 获取病人  执行项目
     * 待执行病人  （进行中的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExecutePro(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryExecutePro(map, currPage, pageSize);
    }

    /**
     * queryOverProject  2016-12-16日  开始使用
     * 获取病人  已完成项目
     * 待执行病人  （已完成项目的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryOverProject(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryOverProject(map, currPage, pageSize);
    }

    /**
     * 获取病人  执行项目  2016-12-16日  停止使用
     * 已完成  病人  （已完成的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryCompleteExecutePro(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryCompleteExecutePro(map, currPage, pageSize);
    }

    /**
     * 获取病人  执行记录   也就是项目的明细
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExecuteRecord(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryExecuteRecord(map, currPage, pageSize);
    }

    /**
     * 获取收费 记录
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryChargeRecord(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryChargeRecord(map, currPage, pageSize);
    }

    /**
     * 查询所有  待执行医嘱  信息
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExcuseDoctor(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.queryExcuseDoctor(map, currPage, pageSize);
    }


    /**
     * 接单时  查询搜索的人员
     * 执行人  配药人  接单人
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryUser(Map<String, Object> paraMap) throws Exception {
        return infusionDao.queryUser(paraMap);
    }

    /**
     * 保存接单 信息
     * member//会员类型   金卡2，普通卡1，钻石卡3
     *
     * @return
     * @throws Exception
     */
    public ModelVo saveOrders(Map<String, Object> vbj1_map,/**病人医嘱执行  基本信息**/
                              List<Map<String, Object>> vaf1_list,  /**病人医嘱记录  list表格**/
                              Map<String, Object> userMap, String vaa01, int memeber, String vaa07
    ) throws Exception {
        ModelVo vo = new ModelVo();
        String msg = "";
        //根据 vaf_list 的值 查询视图 V_VAF_1
        String flag = searchVafView(vaf1_list);

        //判断病人卡上钱是否够用   暂时不用  这个适用于 会员卡

        String param = commonSqlBo.getSysParamValue(9999, 9999, 7);
        if ((memeber == 1 || memeber == 2) && !"0".equals(param)) {
            //如果是会员 先去判断 选中的单据 是否已经交费了，如果交费了，则不用走会员扣费，直接可以进行接单操作
            Map<String, Object> baclistmap = infusionDao.searchChargeCount(vaf1_list);
            List accountlist = (List) baclistmap.get("list");
            if (accountlist.size() > 0 && accountlist != null) {
                //说明有未结账的单据
                Map<String, Object> vai23map = queryVai23(vbj1_map, vaf1_list);
                if (vai23map != null) {
                    //查询看卡上钱 是否够用
                    Map<String, Object> vbuMap = infusionDao.queryCountByVaa01(vaa01);
                    Float sumCount = 0.0f;
                    if (vbuMap != null) {
                        String vbu14 = vbuMap.get("vbu14").toString();
                        String bep06b = vbuMap.get("bep06b").toString();
                        sumCount = Float.parseFloat(vbu14) + Float.parseFloat(bep06b);
                    }
                    List list = (List) vai23map.get("list");
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            Float vai23 = 0.0f;
                            Long vai01 = 0L;
                            Map<String, Object> listmap = (Map) list.get(i);
                            if (listmap != null) {
                                if (!"".equals(listmap.get("fvai23")) && listmap.get("fvai23") != null) {
                                    vai23 = Float.parseFloat(listmap.get("fvai23").toString());
                                }
                                if (!"".equals(listmap.get("vai01")) && listmap.get("vai01") != null) {
                                    vai01 = GlobalVariable.LongIsNull(listmap.get("vai01"));
                                }

                            }
                            if (vai23 > 0) {
                                if (sumCount < vai23) {
                                    //说明钱不够用  给一个提示 说明    病人卡上余额不够，请先到收费处交钱
                                    msg = "病人卡上余额不够，请先到收费处交钱";
                                    vo.setResult(StaticKeys.OH_FALSE);
                                    vo.setResultMsg(msg);
                                    return vo;
                                } else {
                                    //进行扣费的操作  调用总的存储过程
                                    //Map<String,Object> vaiAllMap = queryVaiAll(vbj1_map,vaf1_list);
                                    Map<String, Object> cardMap = new HashMap<String, Object>();
                                    cardMap.put("VAA01", vaa01);
                                    cardMap.put("VAA07", vaa07);
                                    cardMap.put("VAI01", vai01);
                                    cardMap.put("ACF01", 1);
                                    cardMap.put("BCE01", userMap.get("bce01").toString());
                                    cardMap.put("BCk01", userMap.get("depId").toString());
                                    cardMap.put("BCE02", userMap.get("bce02c").toString());
                                    cardMap.put("BCE03", userMap.get("bce03c").toString());
                                    cardMap.put("VAJ38", vai23);
                                    cardMap.put("AType", 0);
                                    cardMap.put("AWorkType", 4);
                                    //调用一卡通扣费的总方法
                                    cardMemberMangerBo.updatehoratecardcharge(cardMap);
                                }
                            }
                        }
                    }

                }
            } else {
                //说明都已经结账了  则不用调用会员扣费方法
            }
        } else {

        }
        if (flag.equals("no")) {  //  说明没有 值 小于8   还没执行或已经作废，不能执行
            saveVbj1(vbj1_map, vaf1_list);
            saveVbi(vaf1_list);//修改医嘱状态
            saveVaf1(vaf1_list, userMap);
        }
        msg = "执行成功";
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setResultMsg(msg);
        return vo;
    }

    /**
     * @param vbj1_map
     * @param vaf1_list
     * @param memeber
     * @return com.cnhis.cloudhealth.commons.Mappers.ModelVo
     * @throws
     * @Title: findSumMoney
     * @Description: （查询扣费总金额）
     * @author chengshaowei
     * @date 2017/9/28
     */
    public ModelVo findSumMoney(Map<String, Object> vbj1_map,/**病人医嘱执行  基本信息**/
                                List<Map<String, Object>> vaf1_list,  /**病人医嘱记录  list表格**/
                                int memeber) throws Exception {
        ModelVo vo = new ModelVo();
        String param = commonSqlBo.getSysParamValue(9999, 9999, 7);
        if ((memeber == 1 || memeber == 2) && !"0".equals(param)) {
            //如果是会员 先去判断 选中的单据 是否已经交费了，如果交费了，则不用走会员扣费，直接可以进行接单操作
            Map<String, Object> baclistmap = infusionDao.searchChargeCount(vaf1_list);
            List accountlist = (List) baclistmap.get("list");
            if (accountlist.size() > 0 && accountlist != null) {
                //说明有未结账的单据
                vbj1_map.put("list", vaf1_list);
                Map<String, Object> vai23map = infusionDao.findSumMoney(vbj1_map);
                vo.setMap(vai23map);
            }
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setResultMsg("金额查询成功");
        } else {
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setResultMsg("无需扣费！");
        }
        return vo;
    }

    public Map<String, Object> queryVai23(Map<String, Object> vbj1_map, List<Map<String, Object>> vaf1_list) throws Exception {
        vbj1_map.put("list", vaf1_list);
        return infusionDao.queryVai23(vbj1_map);
    }

    public Map<String, Object> queryVaiAll(Map<String, Object> vbj1_map, List<Map<String, Object>> vaf1_list) throws Exception {
        vbj1_map.put("list", vaf1_list);
        return infusionDao.queryVaiAll(vbj1_map);
    }


    /**
     * 根据 vaf_list 的值 查询视图 V_VAF_1
     * 循环 判断  vaf10 是否有小于8 的
     */
    public String searchVafView(List<Map<String, Object>> vaf1_list) throws Exception {
        List<Map<String, Object>> list = infusionDao.searchVafView(vaf1_list);
        String flag = "no";//说明没有 值 小于8
        if (list.size() > 0 && list != null) {
            flag = "yes";//说明查询出来有小于 8 的 数据
        }
        return flag;
    }

    /**
     * 保存医嘱 执行 基本信息
     *
     * @param vbj1_map
     * @throws Exception
     */
    public void saveVbj1(Map<String, Object> vbj1_map, List<Map<String, Object>> vaf1_list) throws Exception {
        vbj1_map.put("list", vaf1_list);
        vbj1_map.put("id", getNewId.nextId());
        infusionDao.saveVbj1(vbj1_map);
    }

    /**
     * 修改医嘱状态
     *
     * @param vaf1_list
     * @throws Exception
     */
    public void saveVbi(List<Map<String, Object>> vaf1_list) throws Exception {
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("productid", 100);
        paraMap.put("programid", 107001);
        paraMap.put("paramno", 1);
        SysParameters parameters = clisysParametersDao.queryParameterByPara(paraMap);
        String param3 = "0";
        if (!"".equals(parameters) && parameters != null) {
            if (!"".equals(parameters.getValue()) && parameters.getValue() != null) {
                param3 = parameters.getValue();
            } else {
                param3 = "3";//执行中
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", vaf1_list);
        map.put("param3", param3);
        infusionDao.updateVBI1(map);
        infusionDao.updateVBI1_2(map);
    }

    /**
     * 保存医嘱 记录  到 vaf1表
     *
     * @param vaf1_list
     * @throws Exception
     */
    public void saveVaf1(List<Map<String, Object>> vaf1_list, Map<String, Object> userMap/**病人医嘱记录**/) throws Exception {
        if (vaf1_list != null && vaf1_list.size() > 0) {
            //修改
            userMap.put("list", vaf1_list);
            infusionDao.batchUpdateByVaf(userMap);
        }
    }

    /**
     * 查询  输液单，注射单 等 各个单据  用于打印
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> querySingleinfusion(Map<String, Object> paramMap) throws Exception {
        return infusionDao.querySingleinfusion(paramMap);
    }

    /**
     * 点击执行完成 的操作
     *
     * @return
     * @throws Exception
     */
    public String performOver(List<Map<String, Object>> cbmAndVaf_list) throws Exception {
        return infusionDao.performOver(cbmAndVaf_list);
    }

    /**
     * @param map
     * @return java.lang.String
     * @throws
     * @Title: completes
     * @Description: （分次执行）
     * @author chengshaowei
     * @date 2017/10/12
     */
    public ModelVo completes(Map<String, Object> map) throws Exception {
        //1、获取执行项
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
        for (Map<String, Object> m : list) {
            Integer VBJ05 = GlobalVariable.IntegerIsNull(m.get("vbj05"));//本次次数
            Integer VBI35 = GlobalVariable.IntegerIsNull(m.get("vbi35"));//剩余次数
            m.put("vbi13", 3);
            /**剩余次数减去本次次数**/
            Integer new_VBI35 = VBI35 - VBJ05;
            m.put("vbi35", new_VBI35);
            if (new_VBI35 == 0) {
                m.put("vbi13", 1);
            }
            m.put("vbi01", getNewId.nextId());
            m.put("overday", 0);
            m.put("vbi39", new Date());
            m.put("bce03c", map.get("bce03c"));
            m.put("vaa07", map.get("vaa07"));
        }
        infusionDao.completes(map);
        ModelVo vo = new ModelVo();
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setResultMsg("执行成功");
        return vo;
    }

    /**
     * 退费审核
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryRefundAudit(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return infusionDao.queryRefundAudit(paramMap, currPage, pageSize);
    }

    /**
     * 退费审核中的 人员搜索
     *
     * @param paramMap
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryUserInRefundAudit(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return infusionDao.queryUserInRefundAudit(paramMap, currPage, pageSize);
    }

    /**
     * 根据科室查询  此科室下的所有人员
     * 退费审核
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String, Object> refundAuditByDepId(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return infusionDao.refundAuditByDepId(paramMap, currPage, pageSize);
    }


    /**
     * 点击皮试结果
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> skinTest(Map<String, Object> paramMap) throws Exception {
        return infusionDao.skinTest(paramMap);
    }

    public List<Map<String, Object>> skinTest1(Map<String, Object> paramMap) throws Exception {
        return infusionDao.skinTest1(paramMap);
    }

    /**
     * 基本信息 保存皮试前使用
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryBaseInfo(Map<String, Object> paramMap) throws Exception {
        return infusionDao.queryBaseInfo(paramMap);
    }

    /**
     * 皮试的修改
     * 保存皮试信息   更新vaf
     *
     * @param paramMap
     * @throws Exception
     */
    public void saveSkinTestResult(Map<String, Object> paramMap) throws Exception {
        infusionDao.saveSkinTestResult(paramMap);
    }

    /**
     * 查询是否具有过敏记录
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinRecord(Map<String, Object> paramMap) throws Exception {
        return infusionDao.queryIsHasSkinRecord(paramMap);
    }

    /**
     * 添加过敏记录
     *
     * @param paramMap
     * @throws Exception
     */
    public void addSkinRecord(Map<String, Object> paramMap) throws Exception {
        infusionDao.addSkinRecord(paramMap);
    }

    /**
     * 修改过敏记录
     *
     * @param paramMap
     * @throws Exception
     */
    public void updateSkinRecord(Map<String, Object> paramMap) throws Exception {
        infusionDao.updateSkinRecord(paramMap);
    }

    /**
     * 查询是否具有过敏药物
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinDrug(Map<String, Object> paramMap) throws Exception {
        return infusionDao.queryIsHasSkinDrug(paramMap);
    }

    /**
     * 添加过敏药物
     *
     * @param paramMap
     * @throws Exception
     */
    public void addSkinDrug(Map<String, Object> paramMap) throws Exception {
        infusionDao.addSkinDrug(paramMap);
    }

    /**
     * 退费审核的保存
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> refund(Map<String, Object> map) throws Exception {
        Map<String, Object> msg = new HashMap<String, Object>();
        /**这里需要扣费HORate_CardCharge_Update**/
        for (Map<String, Object> m : (List<Map<String, Object>>) map.get("list")) {
            Map<String, Object> backMap = infusionDao.findAppRefund(m);
            if (backMap != null) {
                msg.put("falg", false);
                msg.put("msg", "费用ID:" + m.get("VAJ01").toString() + "已经审核或拒绝退费，请返回刷新重新提交.");
                return msg;
            } else {
                m.put("BCE01B", map.get("BCE01B"));
                m.put("BCE03B", map.get("BCE03B"));
                m.put("lDate", map.get("lDate"));
                infusionDao.refund1(m);
                infusionDao.refund2(m);
            }
        }
        msg.put("falg", true);
        msg.put("msg", "审核成功");
        return msg;
    }

    /**
     * 根据 vaf01  查询vbj表  查询出  执行时间 vaj08  用于打印
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryVaj08ByVaf01(Map<String, Object> paramMap) throws Exception {
        return infusionDao.queryVaj08ByVaf01(paramMap);
    }

    /**
     * 查询病人 是不是会员
     *
     * @throws Exception
     */
    public Map<String, Object> findMembers(String vaa01) throws Exception {
        return infusionDao.findMembers(vaa01);
    }

    /**
     * 查询病人 会员卡类型
     */
    public Map<String, Object> findMemberCardType(String vaa01) throws Exception {
        return infusionDao.findMemberCardType(vaa01);
    }

    /**
     * 根据 科室 和  vaf01 查询是否可以申请  是本科室给药途径退费
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryApplyRefuse(Map<String, Object> paramMap) throws Exception {
        return infusionDao.queryApplyRefuse(paramMap);
    }

    /**
     * 院内消息查询
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> yuanNeiNotice(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return infusionDao.yuanNeiNotice(map, currPage, pageSize);
    }


}
